package yxy.game.pm2.module.office;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import yxy.apple.util.RandomUtility;
import yxy.apple.util.RandomUtility.Weight;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.conscript;
import yxy.cherry.data.bean.conscriptswap;
import yxy.cherry.data.bean.herodata;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

public class OfficeHandle {

	/** 获取需求物品数量 */
	public Collection<PackageMessage.Item> getItems(String playerCode) {
		ArrayList<Currency> items = new ArrayList<>();
		try {
			Currency item = Item.get(playerCode, CurrencyType.CT_物品, 1023);
			items.add(item);
		} catch (Exception e) {
		}
		try {
			Currency item = Item.get(playerCode, CurrencyType.CT_物品, 1028);
			items.add(item);
		} catch (Exception e) {
		}
		return new CurrencyHandle().packItems(items);
	}

	/** 获得可以置换的武将 */
	public Collection<HeroMessage.HeroSimple> heros(String playerCode) {
		HeroHandle heroHandle = new HeroHandle();
		ArrayList<HeroMessage.HeroSimple> list = new ArrayList<>();
		for (Hero hero : heroHandle.getFreeHeros(playerCode).values()) {
			herodata _herodata = DataCenter.getData(hero.getDataId(), herodata.class);
			if (hero.getStar() < 4 || hero.getStar() > 5 || _herodata.getPower() > 4) {
				continue;
			}
			try {
				list.add(heroHandle.getHeroSimple(playerCode, hero));
			} catch (Exception e) {
			}
		}
		return list;
	}

	public Currency summon(String playerCode, int type) {
		ArrayList<SummonData> summons = new ArrayList<>();
		Collection<conscript> datas = DataCenter.getAllData(conscript.class).values();
		for (conscript data : datas) {
			if (data.getType() == type) {
				summons.add(new SummonData(data));
			}
		}
		if (summons.size() <= 0) {
			return null;
		}
		SummonData one = RandomUtility.randomWeight(summons);
		Reward reward = one.getReward();

		return reward.reward;
	}

	public int change(String playerCode, Hero hero) {
		herodata _herodata = DataCenter.getData(hero.getDataId(), herodata.class);
		int star = hero.getStar();
		int camp = _herodata.getPower();
		Collection<conscriptswap> datas = DataCenter.getAllData(conscriptswap.class).values();

		ArrayList<Change> rewards = new ArrayList<>();

		for (conscriptswap data : datas) {
			if (data.getStartcs() == star && data.getPower() == camp) {
				for (int i = 0; i < data.getRewardid().length; i++) {
					int id = data.getRewardid()[i];
					int weight = data.getPercent()[i];
					if (id > 0 && weight > 0) {
						rewards.add(new Change(id, weight));
					}
				}
			}
		}
		if (rewards.size() <= 0) {
			return 0;
		}

		Change reward = RandomUtility.randomWeight(rewards);
		for (int i = 0; i < 10; i++) {
			if (reward.id != hero.getDataId()) {
				break;
			}
			reward = RandomUtility.randomWeight(rewards);
		}
		return reward.id;
	}

	private class SummonData implements Weight {

		private conscript data;

		private SummonData(conscript data) {
			this.data = data;
		}

		private Reward getReward() {
			List<Reward> rewards = new ArrayList<>();
			for (int i = 0; i < data.getRewardid().length; i++) {
				Currency reward = Currency.create(data.getRewardtype()[i], data.getRewardid()[i], data.getRewardnum()[i]);
				int weight = data.getPercent()[i];
				if (reward != null && weight > 0) {
					rewards.add(new Reward(reward, weight));
				}
			}
			return RandomUtility.randomWeight(rewards);
		}

		@Override
		public int getWeight() {
			return data.getWeight();
		}

	}

	private class Reward implements Weight {

		Currency reward;
		int weight;

		private Reward(Currency reward, int weight) {
			this.reward = reward;
			this.weight = weight;
		}

		@Override
		public int getWeight() {
			return weight;
		}
	}

	private class Change implements Weight {

		int id;
		int weight;

		private Change(int id, int weight) {
			this.id = id;
			this.weight = weight;
		}

		@Override
		public int getWeight() {
			return weight;
		}
	}

}
